package beautiful.butterfly.drds.data_exchange.communicator;


import beautiful.butterfly.drds.data_exchange.constant.Constants;
import beautiful.butterfly.drds.data_exchange.constant.State;
import beautiful.butterfly.drds.data_exchange.report.Collector;
import beautiful.butterfly.drds.data_exchange.report.Message;
import beautiful.butterfly.drds.data_exchange.report.Reporter;
import beautiful.butterfly.drds.data_exchange.util.Configuration;

import java.util.List;
import java.util.Map;

public abstract class AbstractContainerCommunicator
{
    private Configuration configuration;
    private Collector collector;
    private Reporter reporter;

    private Integer jobId;


    private long lastReportTime = System.currentTimeMillis();


    public AbstractContainerCommunicator(Configuration configuration)
    {
        this.configuration = configuration;
        this.jobId = configuration.getInt(Constants.container_job_id);
    }

    public Configuration getConfiguration()
    {
        return this.configuration;
    }

    public Collector getCollector()
    {
        return collector;
    }

    public void setCollector(Collector collector)
    {
        this.collector = collector;
    }

    public Reporter getReporter()
    {
        return reporter;
    }

    public void setReporter(Reporter reporter)
    {
        this.reporter = reporter;
    }

    public Integer getJobId()
    {
        return jobId;
    }

    public abstract void registerCommunication(List<Configuration> configurationList);

    public abstract Message collect();

    public abstract void report(Message message);

    public abstract State collectState();

    public abstract Message getCommunication(Integer id);

    /**
     * 当 实现是 TGContainerCommunicator 时，返回的 Map: key=taskId, value=Message
     * 当 实现是 JobContainerCommunicator 时，返回的 Map: key=taskGroupId, value=Message
     */
    public abstract Map<Integer, Message> getTaskIdToMessageMap();

    public void resetCommunication(Integer id)
    {
        Map<Integer, Message> map = getTaskIdToMessageMap();
        map.put(id, new Message());
    }

    public void reportVmInfo()
    {
        long now = System.currentTimeMillis();
        //每5分钟打印一次
        if (now - lastReportTime >= 300000)
        {

            lastReportTime = now;
        }
    }
}